In [1]:
from __future__ import division, print_function, absolute_import

import tflearn
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.normalization import local_response_normalization
from tflearn.layers.estimator import regression

# Data loading and preprocessing
import tflearn.datasets.mnist as mnist
X, Y, testX, testY = mnist.load_data(one_hot=True)


Extracting mnist/train-images-idx3-ubyte.gz
Extracting mnist/train-labels-idx1-ubyte.gz
Extracting mnist/t10k-images-idx3-ubyte.gz
Extracting mnist/t10k-labels-idx1-ubyte.gz

In [2]:
# Building convolutional network
network = input_data(shape=[None, 784], name='input')
network = fully_connected(network, 500, activation='relu' ,regularizer='L2', weight_decay=0.001)
network = fully_connected(network, 500, activation='relu',regularizer='L2', weight_decay=0.001 )
network = fully_connected(network, 500, activation='relu',regularizer='L2', weight_decay=0.001 )
network = fully_connected(network, 500, activation='relu' ,regularizer='L2', weight_decay=0.001)
network = fully_connected(network, 200, activation='relu' )
network = fully_connected(network, 100, activation='relu' )
network = fully_connected(network, 10, activation='softmax')
network = regression(network, optimizer='adam', learning_rate=0.0001,
                     loss='categorical_crossentropy', name='target')

# Training
model = tflearn.DNN(network, tensorboard_verbose=0)
model.fit({'input': X}, {'target': Y}, n_epoch=20,
           validation_set=({'input': testX}, {'target': testY}),
           snapshot_step=100, show_metric=True, run_id='convnet_mnist_7')


Training Step: 25799  | total loss: 1.59809 | time: 29.674s
| Adam | epoch: 030 | loss: 1.59809 - acc: 0.9175 -- iter: 54976/55000
Training Step: 25800  | total loss: 1.43839 | time: 31.134s
| Adam | epoch: 030 | loss: 1.43839 - acc: 0.9257 | val_loss: 0.16743 - val_acc: 0.9731 -- iter: 55000/55000
--

Beginning:

sklearn logistic regression acc = %84

Second try:

  • 3 hidden units all relu
  • learning rate = 0.001

dev set = %97 test set = %98,714

Third try:

  • 3 hidden unit all relu
  • learning rate = 0.0001

dev_set = %99,29 test_set = %98,557

4th try:

  • 4 hidden unit (500-500-200-100) all relu
  • learning rate = 0.001

dev_set = %96,45 test set = %98,928

5th try:

  • 4 hidden unit (500-500-200-100) all relu
  • learning rate = 0.0001

dev_set = %97,13 test set = %99,057

6th try:

  • 7 hidden unit (500-500-500-500-200-100) all relu
  • learning rate = 0.00001

dev_set = %87,73 test set = %92,842

7th try:

  • 7 hidden unit (500-500-500-500-200-100) all relu
  • learning rate = 0.0001

dev_set = %87,73 test set = %92,842


In [3]:
import pandas as pd 
import numpy as np 
sub_data = pd.read_csv('test.csv')
sub_data.head()


Out[3]:
pixel0 pixel1 pixel2 pixel3 pixel4 pixel5 pixel6 pixel7 pixel8 pixel9 ... pixel774 pixel775 pixel776 pixel777 pixel778 pixel779 pixel780 pixel781 pixel782 pixel783
0 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0

5 rows × 784 columns


In [4]:
test = sub_data.as_matrix()

In [5]:
test = test/255

In [6]:
preds = model.predict(test)

In [7]:
p = []
for i in range(len(test)):
    p.append(np.argmax(preds[i]))

In [8]:
df = pd.DataFrame(p)
df.index.name='ImageId'
df.index+=1
df.index.name='ImageId'
df.columns=['Label']
df.to_csv('results.csv', header=True)

In [ ]: